﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileManager_Server.BusinessLogic.WCF.ObjectContainer_Setting;
using FileManager_Server.BusinessLogic.WCF.System_Setting_Service;
using FileManager_Server.Utility;

namespace FileManager_Server.PresentationLayer
{
    public partial class SetClientVersion : Form
    {
        public SetClientVersion()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
        }

        private void SetClientVersion_Shown(object sender, EventArgs e)
        {
            var applicationInfo = ServerLocalFile.ReadApplicationInfo();
            if(null != applicationInfo)
            {
                var versionCollection = applicationInfo.ClientVersion.Split('.');
                Tb_Version1.Text = versionCollection[0];
                Tb_Version2.Text = versionCollection[1];
                Tb_Version3.Text = versionCollection[2];
                Tb_Version4.Text = versionCollection[3];
                if (null != applicationInfo.Infomation&&applicationInfo.Infomation.Length > 0)
                    Rtb_UpdateInfo.Text = Encoding.UTF8.GetString(applicationInfo.Infomation);
            }
        }

        private void Btn_ConfirmClientVersion_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(Tb_Version1.Text) 
            ||string.IsNullOrEmpty(Tb_Version2.Text) 
                ||string.IsNullOrEmpty(Tb_Version3.Text) 
                ||string.IsNullOrEmpty(Tb_Version4.Text) )
            {
                MessageBox.Show("请输入完整的版本号!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                if(string.IsNullOrEmpty(Rtb_UpdateInfo.Text.Trim()))
                {
                    MessageBox.Show("请输入更新信息!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                var applicationInfo = new ClientApplicationInfoLocal();
                var version = string.Format("{0}.{1}.{2}.{3}", Tb_Version1.Text, Tb_Version2.Text, Tb_Version3.Text,
                                            Tb_Version4.Text);
                applicationInfo.ClientVersion = version;
                applicationInfo.Infomation = Encoding.UTF8.GetBytes(Rtb_UpdateInfo.Text);
                ServerLocalFile.SaveApplicationInfo(applicationInfo);
                if(MessageBox.Show("保存版本号成功，是否需要立即客户端更新!", "修改成功", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
                {
                    ObjectContainerInstance.Instance.UnityObject_Container.GetInterfaceService<ISystemService>().UpdateClientService(version);
                }
                Close();
            }
        }

        private void Tb_Version1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if(e.KeyChar == 46)
            {
                SendKeys.Send("{Tab}");
            }
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
                e.Handled = true;
        }
    }
}
